﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Specialized;
using System.Web;
using Website.Infrastructure;

namespace IlseLeijten.Tests
{
    [TestClass]
    public class RedirectionManagerTests
    {
        [TestMethod]
        public void NoLanguageCheckForNlDomain()
        {
            // Arrange
            var redirectManager = new RedirectionManager();
            // Strict will fail if any method on httpRequest is called that was not setup
            var httpRequest = new Mock<HttpRequestBase>(MockBehavior.Strict);
            var cookieCollection = new HttpCookieCollection();

            // if it's not a .com domain, the accept-language mime header should not be checked
            httpRequest.SetupGet(x => x.Url).Returns(new Uri("http://www.ilseleijten.nl/"));
            httpRequest.SetupGet(x => x.Cookies).Returns(cookieCollection);

            // Act
            bool result = redirectManager.RedirectionRequired(httpRequest.Object);

            // Assert
            Assert.IsFalse(result);
            httpRequest.VerifyAll();
        }

        [TestMethod]
        public void RedirectionForDutchLanguageCookie()
        {
            // Arrange
            var redirectManager = new RedirectionManager();
            var httpRequest = new Mock<HttpRequestBase>();

            var cookieCollection = new HttpCookieCollection();
            cookieCollection.Add(new HttpCookie("SiteLanguage", "nl-nl"));

            httpRequest.SetupGet(x => x.Url).Returns(new Uri("http://www.ilseleijten.com/"));
            httpRequest.SetupGet(x => x.Cookies).Returns(cookieCollection);

            // Act
            bool result = redirectManager.RedirectionRequired(httpRequest.Object);

            // Assert
            Assert.IsTrue(result);
            httpRequest.VerifyAll();
        }

        [TestMethod]
        public void RedirectionForDutchLanguageHeader()
        {
            // Arrange
            var redirectManager = new RedirectionManager();
            var httpRequest = new Mock<HttpRequestBase>();

            var cookieCollection = new HttpCookieCollection();
            cookieCollection.Add(new HttpCookie("SiteLanguage", "en-US"));

            var headerCollection = new NameValueCollection();
            headerCollection.Add("Accept-Language", "nl-NL");

            httpRequest.SetupGet(x => x.Url).Returns(new Uri("http://www.ilseleijten.com/"));
            httpRequest.SetupGet(x => x.Cookies).Returns(cookieCollection);
            httpRequest.SetupGet(x => x.Headers).Returns(headerCollection);

            // Act
            bool result = redirectManager.RedirectionRequired(httpRequest.Object);

            // Assert
            Assert.IsTrue(result);
            httpRequest.VerifyAll();
        }

        [TestMethod]
        public void RedirectionForMultpleLanguageHeader()
        {
            // Arrange
            var redirectManager = new RedirectionManager();
            var httpRequest = new Mock<HttpRequestBase>();

            var cookieCollection = new HttpCookieCollection();
            cookieCollection.Add(new HttpCookie("SiteLanguage", "en-US"));

            var headerCollection = new NameValueCollection();
            headerCollection.Add("Accept-Language", "da-DK,nl-NL;q=0.7,en-US;q=0.3");

            httpRequest.SetupGet(x => x.Url).Returns(new Uri("http://www.ilseleijten.com/"));
            httpRequest.SetupGet(x => x.Cookies).Returns(cookieCollection);
            httpRequest.SetupGet(x => x.Headers).Returns(headerCollection);

            // Act
            bool result = redirectManager.RedirectionRequired(httpRequest.Object);

            // Assert
            Assert.IsTrue(result);
            httpRequest.VerifyAll();
        }

        [TestMethod]
        public void GetAlternateUrlForDotCom()
        {
            // Arrange
            var redirectManager = new RedirectionManager();
            var request = new Mock<HttpRequestBase>();
            request.SetupGet(x => x.Url)
                .Returns(() => new Uri("http://www.ilseleijten.com/"));

            // Act
            string url = redirectManager.GetAlternateSiteUrl(request.Object);

            // Assert
            Assert.AreEqual("http://www.ilseleijten.nl/", url);
        }

        [TestMethod]
        public void GetAlternateUrlForDotNl()
        {
            // Arrange
            var redirectManager = new RedirectionManager();
            var request = new Mock<HttpRequestBase>();
            request.SetupGet(x => x.Url)
                .Returns(() => new Uri("http://www.ilseleijten.nl/"));

            // Act
            string url = redirectManager.GetAlternateSiteUrl(request.Object);

            // Assert
            Assert.AreEqual("http://www.ilseleijten.com/", url);
        }

        [TestMethod]
        public void GetAlternateUrlForFullUrl()
        {
            // Arrange
            var redirectManager = new RedirectionManager();
            var request = new Mock<HttpRequestBase>();
            request.SetupGet(x => x.Url)
                .Returns(() => new Uri("http://www.ilseleijten.com/About"));

            // Act
            string url = redirectManager.GetAlternateSiteUrl(request.Object);

            // Assert
            Assert.AreEqual("http://www.ilseleijten.nl/About", url);
        }
    }
}
